#!/usr/bin/perl -w
use strict;
use feature 'switch';
use HuginnDaemon qw( IsRunning Daemonize Terminate DaemonName MainDaemonStatusPhp);

use HuginnConfiguration qw( %Config );
use HuginnCommunications qw( SendEmail SendSMS GnokiiGenerateConfig SendFax);
my $command = $ARGV[0] || "usage";
$Config{DAEMON_NAME} = $Config{DAEMON_STATUS_PHP};

given($command)
{
    when ("start")        {&DaemonStart();}
    when ("stop")         {&DaemonStop();}
    when ("status")       {&DaemonStatus();}
    when ("usage")        {&Usage();}
    when ("force-action") {&DaemonForceAction();}
    default               {&Usage(); print "Unknow command $command\n";    }
}
sub DaemonStart()
{
    Daemonize(\&MainDaemonStatusPhp) unless IsRunning(DaemonName() );
    IsRunning(DaemonName()) or die "Unable to start daemon. Check log";
}
sub DaemonStop()
{
    Terminate(DaemonName()) if IsRunning(DaemonName());
    IsRunning(DaemonName()) and die "Unable to stop daemon. Check log";
}
sub DaemonStatus()
{
    print (IsRunning(DaemonName())?"started\n":"stopped\n");
}
sub DaemonForceAction()
{
    if (IsRunning(DaemonName()))
    {
        HuginnDaemon::DaemonComunication(DaemonName(),10) ;    
    }
    else
    {
        print "Daemon not running.\n";
    }
}
sub Usage()
{
    print "Usage: $0 <start | stop | status | force-action | usage >\n";
}


